﻿using System;
using Newtonsoft.Json;

namespace Ext.Direct.Mvc
{
    [JsonObject]
    public class DirectResponse
    {
        public const string ResponseExceptionType = "exception";

        public DirectResponse() { }

        public DirectResponse(DirectRequest request) {
            this.Type = request.Type;
            this.TransactionId = request.TransactionId;
            this.Action = request.Action;
            this.Method = request.Method;
            this.IsUpload = request.IsUpload;
        }

        public DirectResponse(DirectRequest request, Exception exception)
            : this(request) {
            this.Type = ResponseExceptionType;
            this.ExceptionMessage = exception.Message;
            this.Where = String.Format("{0}.{1}", request.Action, request.Method);
        }

        [JsonProperty("type")]
        public string Type {
            get;
            set;
        }

        [JsonProperty("tid")]
        public int TransactionId {
            get;
            set;
        }

        [JsonProperty("action")]
        public string Action {
            get;
            set;
        }

        [JsonProperty("method")]
        public string Method {
            get;
            set;
        }

        [JsonProperty("result")]
        public object Result {
            get;
            set;
        }

        [JsonProperty("message", NullValueHandling = NullValueHandling.Ignore)]
        public string ExceptionMessage {
            get;
            set;
        }

        [JsonProperty("where", NullValueHandling = NullValueHandling.Ignore)]
        public string Where {
            get;
            set;
        }

        [JsonIgnore]
        public bool IsUpload {
            get;
            private set;
        }
    }
}
